package com.pfizer.XmlContentHandler;

import static com.pfizer.Entity.Handout.HANDOUTCLASSSTRING;
import static com.pfizer.Entity.Handout.HANDOUTFESTRING;
import static com.pfizer.Entity.Handout.HANDOUTICONSTRING;
import static com.pfizer.Entity.Handout.HANDOUTIDSTRING;
import static com.pfizer.Entity.Handout.HANDOUTLUTIMESTRING;
import static com.pfizer.Entity.Handout.HANDOUTPICCNTSTRING;
import static com.pfizer.Entity.Handout.HANDOUTQNSTRING;
import static com.pfizer.Entity.Handout.HANDOUTSTRING;
import static com.pfizer.Entity.Handout.HANDOUTSUBJSTRING;
import static com.pfizer.Entity.Handout.HANDOUTURLPATHSTRING;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.pfizer.Entity.Handout;

/**
 * HandoutContentHandler--<b>解析文章XML數據</b> 本類用於完成解析XML信息，將解析后的XML放於list中供外部訪問
 * 
 * @author krui
 * @version 1.0 2011/11/21
 */
public class HandoutContentHandler extends DefaultHandler {
	private List<Handout> handouts;
	private String sUrlPath = "";
	private Handout handout;
	private StringBuilder sb = new StringBuilder();

	/**
	 * 解析XML數據放于List中，此方法用於外部獲得解析后的數據
	 * 
	 * @return List<Handout>
	 */
	public List<Handout> getHandouts() {
		return handouts;
	}

	/**
	 * 在根標籤開始時就實例化list用於存放每組數據
	 */
	@Override
	public void startDocument() throws SAXException {
		handouts = new ArrayList<Handout>();
	}

	/**
	 * 開始解析數據，將得到的數據放於javabean
	 * 
	 */
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		super.characters(ch, start, length);
		if(ch[0]=='\n'&&length==1)
		{
			return;
		}
		String sTmp = new String(ch, start, length);
		sTmp=sTmp.trim();
		if(sTmp.length()==0)
		{
			return;
		}
		// (2)不管在startElement到endElement的过程中，执行了多少次characters，
		// 都会将内容添加到StringBuilder中，不会丢失内容
		sb.append(sTmp);
	}

	/**
	 * 在一個tag開始時實例化組對象，
	 */
	@Override
	public void startElement(String uri, String localName, String name,
			Attributes attributes) throws SAXException {
		if (HANDOUTSTRING.equals(localName)) {
			handout = new Handout();
			if (sUrlPath.trim().length() > 0) {
				handout.setUrlPath(sUrlPath);
			}
		}
	}

	/**
	 * 在一個tag結束后將本組數據放於list
	 */
	@Override
	public void endElement(String uri, String localName, String name)
			throws SAXException {
		if (HANDOUTSTRING.equals(localName) && handout != null) {
			if(handout.getLstUpdTime()>0&&handout.getHandoutClass().length()>0)
			{
				handouts.add(handout);
			}
			handout = null;
		} else {
			String valueString = sb.toString();
			if (HANDOUTURLPATHSTRING.equals(localName)) {
				sUrlPath=valueString;
			}
			if (handout != null) {
				if (HANDOUTIDSTRING.equals(localName)) {
					handout.setID(valueString);
				} else if (HANDOUTICONSTRING.equals(localName)) {
					handout.setIcon(valueString);
				} else if (HANDOUTSUBJSTRING.equals(localName)) {
					handout.setSubj(valueString);
				} else if (HANDOUTFESTRING.equals(localName)) {
					handout.setFilenameExtension(valueString);
				} else if (HANDOUTPICCNTSTRING.equals(localName)) {
					handout.setPicCnt(new Integer(valueString).intValue());
				} else if (HANDOUTCLASSSTRING.equals(localName)) {
					handout.setHandoutClass(valueString);
				} else if (HANDOUTQNSTRING.equals(localName)) {
					handout.setQuestionnaire(valueString);
				} else if (HANDOUTLUTIMESTRING.equals(localName)) {
					handout.setLstUpdTime(valueString);
				}
			}
		}
		sb.delete( 0, sb.length());
	}
}
